package uns.fit.gis.ui;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

//import com.sun.midp.midlet.Selector;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.Util;
import uns.fit.gis.fileio.FileWrapper;
import uns.fit.gis.i18n.LocaleManager;
import uns.fit.gis.mapdrawer.ActionCode;
import uns.fit.gis.mapdrawer.LayerConfig;
import uns.fit.gis.mapdrawer.MapCanvas;
import uns.fit.gis.mapobj.SimpleLocation;

public class SaveCurPosForm extends Form implements CommandListener, ItemStateListener {
	private TextField note = new TextField(LocaleManager.getMessage("note"), "My POI", 300, TextField.ANY);
	//private TextField position = new TextField("Position", "", 100, TextField.UNEDITABLE);
	private static String[] coordinateTypes = {LocaleManager.getMessage("gps"), LocaleManager.getMessage("cursor")};
	private ChoiceGroup coordinateTypeChoiceGroup = new ChoiceGroup(LocaleManager.getMessage("coordinatetype"), ChoiceGroup.EXCLUSIVE, coordinateTypes, null);
	private static String[] locationTypes = {	LocaleManager.getMessage("gasstation"),
												LocaleManager.getMessage("atm"),
												LocaleManager.getMessage("school"),
												LocaleManager.getMessage("store"),
												LocaleManager.getMessage("cafe"),
												LocaleManager.getMessage("constructionsite"),
												LocaleManager.getMessage("other")};
	private ChoiceGroup locationTypeChoiceGroup = new ChoiceGroup(LocaleManager.getMessage("locationtype"), ChoiceGroup.EXCLUSIVE, locationTypes, null);
	private TextField otherType = new TextField("", "", 100, TextField.ANY);
	private Command backCommand = new Command(LocaleManager.getMessage("back"), Command.BACK, 1);
	private Command saveCommand = new Command(LocaleManager.getMessage("save"), Command.OK, 1);
	public SaveCurPosForm() {
		super(LocaleManager.getMessage("title"));
		
		//append(this.position);
		append(note);
		append(locationTypeChoiceGroup);
		append(coordinateTypeChoiceGroup);
		
		addCommand(backCommand);
		addCommand(saveCommand);
		//setPosition();
		setCommandListener(this);
	}
	
	/*
	public void setPosition() {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(MapCanvas.cursorCenterX).append(" ").append(MapCanvas.cursorCenterY);
		position.setString(strBuf.toString());
		strBuf = null;
	}
	*/
	/*
	public void setPosition(String position) {
		this.position.setString(position);
	}
	*/
	public void commandAction(Command command, Displayable displayable) {
		if(command == backCommand) {
			//MapDrawer.getMidlet().mapCanvas.commandAction(ActionCode.SHOW_MAP);
			MapDrawer.getMidlet().showMapCanvas();
		}
		else if (command == saveCommand) {
			/*
			new Thread() {
				public void run() {
					FileWrapper.saveUserLocation(Util.STORAGE_FOLDER + Util.USER_LOCATION_FILE_NAME, note.getString(), MapCanvas.cursorCenterX, MapCanvas.cursorCenterY);
				}
			}.start();
			MapDrawer.getMidlet().getSettings().setNumberOfUserLocations(MapDrawer.getMidlet().getSettings().getNumberOfUserLocations() + 1);
			*/
			SimpleLocation location = new SimpleLocation();
			/*
			 * In fact, setting id is not important 
			 */
			location.setId(0);
			switch(locationTypeChoiceGroup.getSelectedIndex()) {
			case 0:
				location.setType(LayerConfig.LAYER_GASSTATION);
				break;
			case 1:
				location.setType(LayerConfig.LAYER_ATM);
				break;
			case 2:
				location.setType(LayerConfig.LAYER_SCHOOL);
				break;
			case 3:
				location.setType(LayerConfig.LAYER_STORE);
				break;
			case 4:
				location.setType(LayerConfig.LAYER_CAFE);
				break;
			case 5:
				location.setType(LayerConfig.LAYER_CONSTRUCTIONSITE);
				break;
			case 6:
				location.setType(LayerConfig.LAYER_OTHER_LOCATION);
				break;
			default:
				break;
			}
			if(coordinateTypeChoiceGroup.getSelectedIndex() == 1) {
				location.setX(MapCanvas.cursorCenterX);
				location.setY(MapCanvas.cursorCenterY);
			}
			else {
				location.setX(MapCanvas.currentX);
				location.setY(MapCanvas.currentY);
			}
			location.setName(Util.encodeStringToBytes(note.getString()));
			MapCanvas.userLocations.addElement(location);
			/*
			new Thread() {
    			public void run() {
    				MapCanvas.userLocations = FileWrapper.loadUserLocations(Util.STORAGE_FOLDER + Util.USER_LOCATION_FILE_NAME);
    			}
    		}.start();
    		*/
			if(location.getType() == LayerConfig.LAYER_CONSTRUCTIONSITE) {
				int segmentId = Util.findClosestSegment2(location.getX(), location.getY());
				if(segmentId != -1) {
					MapCanvas.constructionSiteSegmentIds.addElement(new Integer(segmentId));
				}
			}
			MapDrawer.getMidlet().showMapCanvas();
		}
	}

	public void itemStateChanged(final Item item) {
		if(item == locationTypeChoiceGroup) {
			if(locationTypeChoiceGroup.getSelectedIndex() == locationTypes.length - 1) {
				
			}
		}
		
	}
}
